#coding:utf-8

import scrapy
from ..items import TencentItem

class TencentSpider(scrapy.Spider):
    name = "tencent"

    allowed_domains = ["hr.tencent.com"]

    # python3中可能会出现无法取到类属性,(估计是类属性和实例属性的作用域问题),所以可以添加global
    #global base_url, page
    base_url = "https://hr.tencent.com/position.php?&start="
    page = 0

    start_urls = [base_url + str(page)]


    def parse(self, response):
        node_list = response.xpath("//tr[@class='even'] | //tr[@class='odd']")

        for node in node_list:
            item = TencentItem()

            # 职位名
            item['position_name'] = node.xpath("./td[1]/a/text()").extract_first()
            # 详情链接
            item['position_link'] = "https://hr.tencent.com/" + node.xpath("./td[1]/a/@href").extract_first()
            # 职位类型
            item['position_type'] = node.xpath("./td[2]/text()").extract_first()
            # 招聘人数
            item['people_number'] = node.xpath("./td[3]/text()").extract_first()
            # 工作地点
            item['work_location'] = node.xpath("./td[4]/text()").extract_first()
            # 发布时间
            item['publish_times'] = node.xpath("./td[5]/text()").extract_first()

            #每次迭代提取一组数据item,通过yield交给引擎-管道
            yield item

        #这里采取限定页码
        if self.page <= 100:
            self.page += 10

            #这里还可以构建详情页的请求,但是请求详情页后的解析函数需重新定义如:parse_page()
            # yield scrapy.Request(item['position_link'], callback=self.parse_page)

            #下面对每个page构建请求对象,用于交给引擎--调度器--下载器处理,并将响应传递给callback指定的回调函数解析
            yield scrapy.Request(url=self.base_url+str(self.page), callback=self.parse)

    # def parse_page(self, response):
    #     url = resoonse.xpath()
    #         ...
    # 这里仍然可以考虑请求响应后的回调处理
    #     yield scrapy.Request(url, callback=self.parse)
